package cn.com.geoway.booksystem.controller.admin;


import cn.com.geoway.booksystem.dao.BookBorrowDao;
import cn.com.geoway.booksystem.dao.BookDao;
import cn.com.geoway.booksystem.entity.Book;
import cn.com.geoway.booksystem.entity.Borrow;
import cn.com.geoway.booksystem.entity.User;
import cn.com.geoway.booksystem.service.BookService;
import cn.com.geoway.booksystem.service.BorrowService;
import cn.com.geoway.booksystem.util.StringUtil;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/bookborrow")
public class Admin_BookBorrow_Controller {

    @Resource
    private BorrowService borrowService;
    @Resource
    private BookBorrowDao bookBorrowDao;
    @Resource
    private BookDao bookDao;



    /**
     *  借阅审批
     */
    @ResponseBody
    @RequestMapping("/update")
    public JSONObject update(@RequestParam(value = "borrowId", required = false) Integer borrowId,
                             @RequestParam(value = "record", required = false) Integer record,
                             HttpServletResponse response,
                             HttpServletRequest request)throws Exception {
        JSONObject result = borrowService.update(borrowId,record);
        return result;
    }

    /**
     * 总的借阅记录
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list(@RequestParam(value = "page", required = false) Integer page,
                                    @RequestParam(value = "limit", required = false) Integer limit,
                                    @RequestParam(value = "record", required = false) Integer record,
                                    @RequestParam(value = "bookname", required = false) String bookname,
                                    @RequestParam(value = "username", required = false) String username,
                                    @RequestParam(value = "btype", required = false) Integer btype,
                                    HttpServletResponse response,
                                    HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Book book = null;
        if(StringUtil.isNotEmpty(bookname))
            book = bookDao.findByName(bookname);

        if(!(username == null || username.equals("")))
            map.put("username",username);
        if(!(record == null || record.equals("")))
            map.put("record",record);
        if(!(btype == null || btype.equals("")))
            map.put("type",btype);
        if(!(book == null || book.equals("")))
            map.put("book",book);


        List<Borrow> list = borrowService.list(map, page-1, limit);
        long total = borrowService.getTotal(map);

        map.put("data", list);
        map.put("count", total);
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }


    @ResponseBody
    @RequestMapping("/list01")
    public List<Book> list() throws Exception {
        List<Book> byTypeId = bookDao.findByTypeId(1);
        return byTypeId;
    }

    /**
     * 个人借阅记录
     */
    @ResponseBody
    @RequestMapping("/list1")
    public Map<String, Object> list1(@RequestParam(value = "page", required = false) Integer page,
                                    @RequestParam(value = "limit", required = false) Integer limit,
                                     @RequestParam(value = "q", required = false) String username,
                                    HttpServletResponse response,
                                    HttpServletRequest request) throws Exception {
       // User currentUser = (User) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        //Integer id = currentUser.getId();
        Map<String, Object> map = new HashMap<String, Object>();
        //map.put("user",currentUser);
        map.put("username",username);

        List<Borrow> list = borrowService.list1(map, page-1, limit);
        long total = borrowService.getTotal1(map);

        map.put("data", list);
        map.put("count", total);
        map.put("code", 0);
        map.put("msg", "");
        return map;
    }

    /**
     * 归还书籍
     */
    @ResponseBody
    @RequestMapping("/returnbook")
    public JSONObject returnbook(@RequestParam(value = "borrowID", required = false) Integer borrowID,
                                     HttpServletResponse response,
                                     HttpServletRequest request) throws Exception {

        JSONObject result = borrowService.returnbook(borrowID);
        return result;

    }



}
